package com.ckr.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author Shadowckr
 * @create 2022-01-01 14:20
 */

@Controller
public class RestFulController {
    //映射访问路径
    @RequestMapping("/commit1/{p1}/{p2}")
    public String index1(@PathVariable int p1, @PathVariable int p2, Model model) {

        int result = p1 + p2;
        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg", "结果：" + result);
        //返回视图位置
        return "test";
    }

    //映射访问路径
    @RequestMapping("/commit2/{p1}/{p2}")
    public String index2(@PathVariable int p1, @PathVariable String p2, Model model) {

        String result = p1 + p2;
        //Spring MVC会自动实例化一个Model对象用于向视图中传值
        model.addAttribute("msg", "结果：" + result);
        //返回视图位置
        return "test";
    }

    //映射访问路径，必须是POST请求
    //我们使用浏览器地址栏进行访问默认是Get请求，会报错405
    //HTTP状态 405 - 方法不允许 Request method 'GET' not supported
//    @RequestMapping(value = "/hello",method = {RequestMethod.POST})
//    public String index3(Model model){
//        model.addAttribute("msg", "hello!");
//        return "test";
//    }
    //映射访问路径，必须是Get请求
    @RequestMapping(value = "/hello", method = {RequestMethod.GET})
    public String index3(Model model) {
        model.addAttribute("msg", "hello!");
        return "test";
    }

    @RequestMapping(path = "/ace", method = {RequestMethod.GET})
    public String index4(Model model) {
        model.addAttribute("msg", "Ace!");
        return "test";
    }

    //    @RequestMapping(path = "/hunter",method = {RequestMethod.GET})
    @GetMapping("/hunter")
    public String index5(Model model) {
        model.addAttribute("msg", "Ace,I'm coming home!");
        return "test";
    }

    /*
    所有的地址栏请求默认都会是 HTTP GET 类型的。
    方法级别的注解变体有如下几个：组合注解
    @GetMapping
    @PostMapping
    @PutMapping
    @DeleteMapping
    @PatchMapping
    @GetMapping 是一个组合注解，平时使用的会比较多！
    它所扮演的是 @RequestMapping(method =RequestMethod.GET) 的一个快捷方式。
     */
}
